package cn.peifu.hrm.web.controller;

import cn.peifu.hrm.CourseDoc;
import cn.peifu.hrm.dto.CourseSearchDto;
import cn.peifu.hrm.repository.CourseElasticsearchRepository;
import cn.peifu.hrm.service.ISearchCourseService;
import cn.peifu.hrm.util.AjaxResult;
import cn.peifu.hrm.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EsCourseController {

    @Autowired
    private CourseElasticsearchRepository repository;

    @Autowired
    private ISearchCourseService searchCourseService;

    @RequestMapping(value = "/es/course",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('es:saveCourse')")
    public AjaxResult save2Es(@RequestBody CourseDoc courseDoc){
        repository.save(courseDoc);
        return AjaxResult.me();
    }

    /**
     * 前台查询课程列表
     * @param dto
     * @return
     */
    @RequestMapping(value = "/es/searchCourse",method = RequestMethod.POST)
    public AjaxResult searchCourse(@RequestBody CourseSearchDto dto){
        PageList<CourseDoc> pageList = searchCourseService.searchCourse(dto);
        return AjaxResult.me().setResultObj(pageList);
    }

}
